// eslint.config.ts
import { readFile } from 'node:fs/promises'
import pluginVue from 'eslint-plugin-vue'
import vueParser from 'vue-eslint-parser'
import typeScriptParser from '@typescript-eslint/parser'
import typeScriptPlugin from '@typescript-eslint/eslint-plugin'

const autoImport = JSON.parse((await readFile(new URL('./.eslintrc-auto-import.json', import.meta.url))).toString())

export default [
    {
        ignores: [
            "eslint.config.ts",
            'src/auto-imports.d.ts',
            'src/uni-pages.d.ts',
            'src/components.d.ts',
            'dist/**',
            'node_modules/**',
            'node_modules/**',
            'node_modules/**',
            'build/**'
        ],
    },
    {
        files: ['**/*.js', '**/*.ts', '**/*.tsx'],
        languageOptions: {
            ecmaVersion: 2020,
            sourceType: 'module',
            globals: {
                ...autoImport.globals,
                uni: 'readonly',
                UniNamespace: 'readonly',
                Objable: 'readonly',
                NodeJS: 'readonly',
                AnyFn: 'readonly',
                ResponseData: 'readonly',
                ApiType: 'readonly',
                Nullable: 'readonly',
                App: 'readonly',
                Page: 'readonly',
                console: 'readonly',
                getApp: 'readonly',
                window: 'readonly',
                clearInterval: 'readonly',
                setInterval: 'readonly',
                setTimeout: 'readonly',
                clearTimeout: 'readonly',
                document: 'readonly',
                navigator: 'readonly',
                btoa: 'readonly',
                atob: 'readonly',
            },
        },
        rules: {
            'no-undef': ['error', { typeof: true }],
            'no-unused-vars': 'off',
        },
    },
    {
        // 处理 TypeScript 文件
        files: ['**/*.ts', '**/*.tsx'],
        languageOptions: {
            parser: typeScriptParser,
            parserOptions: {
                ecmaVersion: 2020,
                sourceType: 'module',
                project: './tsconfig.json',
            },
        },
        plugins: {
            '@typescript-eslint': typeScriptPlugin,
        },
        rules: {
            'no-undef': ['error', { typeof: true }],
        },
    },
    {
        // 专门处理 Vue 文件
        files: ['**/*.vue'],
        languageOptions: {
            parser: vueParser,
            parserOptions: {
                ecmaVersion: 2020,
                sourceType: 'module',
                parser: typeScriptParser,
                extraFileExtensions: ['.vue'],
                // project: './tsconfig.json',
            },
            globals: {
                ...autoImport.globals,
                uni: 'readonly',
                UniNamespace: 'readonly',
                Objable: 'readonly',
                NodeJS: 'readonly',
                AnyFn: 'readonly',
                ResponseData: 'readonly',
                ApiType: 'readonly',
                Nullable: 'readonly',
                App: 'readonly',
                Page: 'readonly',
                console: 'readonly',
                getApp: 'readonly',
            },
        },
        plugins: {
            vue: pluginVue,
        },
        rules: {
            'no-undef': ['error', { typeof: true }],
            'vue/no-parsing-error': [
                'error',
                {
                    'abrupt-closing-of-empty-comment': true,
                    'absence-of-digits-in-numeric-character-reference': true,
                },
            ],
            'vue/attributes-order': 'off',
            'vue/require-v-for-key': 'off',
            'vue/no-use-v-if-with-v-for': 'off',
            'vue/valid-v-for': 'off',
            'vue/no-dupe-keys': 'off',
            'vue/no-unused-components': 'off',
            'vue/no-unused-vars': 'off',
            'vue/html-indent': 'off',
            'vue/no-side-effects-in-computed-properties': 'off',
            'vue/return-in-computed-property': 'off',
            'vue/require-valid-default-prop': 'off',
            'vue/require-prop-type-constructor': 'off',
            'vue/component-name-in-template-camel-case': 'off',
        },
    },
]
